//
//  ZCInvolvedNewcommentsCell.m
//  ZCInvolved
//
//  Created by apple on 16/3/21.
//  Copyright © 2016年 MDJ. All rights reserved.
//

#import "ZCInvolvedNewcommentsCell.h"
#import "ZCInvolvedDetailsModelChildComments.h"
@implementation AnswerMenuView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.whc_Edge = UIEdgeInsetsZero;
        self.whc_Orientation = Horizontal;
        _supperNum = [UILabel new];
        _supperView = [UIView new];
        _answerView = [UIView new];
        _supperImg = [UIImageView new];
        _answerImg = [UIImageView new];
        _supperLable = [UILabel new];
        _answerLable = [UILabel new];
        [self addSubview:_supperView];
        [self addSubview:_answerView];
        [self addSubview:_supperNum];
        [_supperView addSubview:_supperLable];
        [_supperView addSubview:_supperImg];
        [_answerView addSubview:_answerLable];
        [_answerView addSubview:_answerImg];
        
        [_supperView whc_LeftSpace:0];
        [_supperView whc_TopSpace:15];
        [_supperView whc_Size:CGSizeMake(60, 25)];
        [_answerView whc_RightSpace:0];
        [_answerView whc_TopSpace:15];
        [_answerView whc_Size:CGSizeMake(60, 25)];
    
        [_supperLable whc_RightSpace:0];
        [_supperLable whc_CenterY:0];
        [_supperLable whc_Size:CGSizeMake(27,21)];
        [_supperImg whc_RightSpace:0 toView:_supperLable];
        [_supperImg whc_CenterY:0];
        [_supperImg whc_Size:CGSizeMake(21,21)];
        
        [_supperNum whc_CenterX:0 toView:_supperImg];
        [_supperNum whc_TopSpace:0];
        [_supperNum whc_Size:CGSizeMake(60,15)];
        
        [_answerLable whc_RightSpace:0];
        [_answerLable whc_CenterY:0];
        [_answerLable whc_Size:CGSizeMake(27,21)];
        [_answerImg whc_RightSpace:0 toView:_answerLable];
        [_answerImg whc_CenterY:0];
        [_answerImg whc_Size:CGSizeMake(21,21)];
        
        _supperImg.image = [UIImage imageNamed:@"projectdetail_zan"];
        _answerImg.image = [UIImage imageNamed:@"projectdetail_comment"];
        _supperLable.text = @"9";
        _answerLable.text = @"10";
        _supperNum.textAlignment = NSTextAlignmentCenter;
        _supperNum.text = @"+1";
        _supperNum.alpha = 0;
        _supperNum.textColor = [UIColor colorWithHexString:@"#ffdd00"];
    
        _supperNum.textColor = [UIColor colorWithHexString:@"8a8a8a"];
        _supperNum.font = [UIFont systemFontOfSize:14];
        
        _answerLable.textColor = [UIColor colorWithHexString:@"8a8a8a"];
        _supperLable.textColor = [UIColor colorWithHexString:@"8a8a8a"];
        _answerLable.font = [UIFont systemFontOfSize:14];
        _supperLable.font = [UIFont systemFontOfSize:14];
        
        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickSupperButton:)];
        [self.supperView addGestureRecognizer:recognizer];
        
        UITapGestureRecognizer *recognizercomment = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickAnswerButton:)];
        [self.answerView addGestureRecognizer:recognizercomment];
    }
    return self;
}


- (void)clickSupperButton:(UIButton *)sender {

    if (_delegate && [_delegate respondsToSelector:@selector(answerMenuView:isClickSupper:)]) {
        [_delegate superMenuView:self isClickSupper:YES];
    }
}

- (void)clickAnswerButton:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(answerMenuView:isClickSupper:)]) {
        [_delegate answerMenuView:self isClickSupper:NO];
    }
    
}

@end

@interface ZCInvolvedNewcommentsCell ()<AnswerMenuViewDelegate> {
    
    UILabel                * _userNameLabel;
    UILabel                * _contentLabel;
    UILabel                * _comentimeLabel;
    UIImageView            * _userImageView;
    UIButton               * _moreButton;
    
    NSMutableArray         * _commentLabelArray;
    NSMutableArray         * _imageViewArray;
    
    WHC_StackView          * _imageStackView;
    WHC_StackView          * _commentStackView;
    AnswerMenuView         * _menuView;
    ZCInvolvedDetailsModelHotComments            * _model;
}

@end

@implementation ZCInvolvedNewcommentsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        CGRect cellFrame = self.contentView.frame;
        cellFrame.size.width = [UIScreen mainScreen].bounds.size.width;
        self.contentView.frame = cellFrame;
        _commentLabelArray = [NSMutableArray array];
        _imageViewArray = [NSMutableArray array];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initLayout];
    }
    return self;
}

- (void)createLayoutView {
    _userImageView = [UIImageView new];
    _userNameLabel = [UILabel new];
    _contentLabel = [UILabel new];
    _imageStackView = [WHC_StackView new];
    _commentStackView = [WHC_StackView new];
    _comentimeLabel = [UILabel new];
    _menuView = [AnswerMenuView new];
    _moreButton = [UIButton new];
    _menuView.delegate = self;
    
    [self.contentView addSubview:_userImageView];
    [self.contentView addSubview:_userNameLabel];
    [self.contentView addSubview:_menuView];
    [self.contentView addSubview:_comentimeLabel];
    [self.contentView addSubview:_contentLabel];
    [self.contentView addSubview:_moreButton];
    [self.contentView addSubview:_commentStackView];
    

    _userNameLabel.textColor = [UIColor colorWithHexString:@"#8a8a8a"];
    _userNameLabel.font = [UIFont systemFontOfSize:14];
    _comentimeLabel.textColor = [UIColor colorWithHexString:@"b2b2b2"];
    _comentimeLabel.font = [UIFont systemFontOfSize:13];
    _commentStackView.backgroundColor = [UIColor colorWithRed:240.0 / 255 green:240.0 / 255 blue:240.0 / 255 alpha:1.0];
    _contentLabel.font = [UIFont systemFontOfSize:14];
    _contentLabel.textColor = [UIColor colorWithHexString:@"#404040"];
    _contentLabel.backgroundColor = [UIColor whiteColor];
    _moreButton.backgroundColor = _commentStackView.backgroundColor;
    _moreButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _moreButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _moreButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    _moreButton.enabled =NO;
    
    [_moreButton setTitle:@"查看更多评论" forState:UIControlStateNormal];
    [_moreButton setTitleColor:MainColor forState:UIControlStateNormal];
}


- (void)initLayout {
    [self createLayoutView];
    
    /// 使用先进的WHC_AutoLayoutKit进行自动布局
    
    [_menuView whc_RightSpace:18];
    [_menuView whc_TopSpace:0];
    [_menuView whc_Size:CGSizeMake(120, 40)];
    
    [_userImageView whc_Frame:WHCRectMake(18, 17, 45, 45)];
    _userImageView.layer.cornerRadius = 45/2;
    _userImageView.layer.masksToBounds = YES;
    
    [_userNameLabel whc_TopSpaceEqualView:_userImageView];
    [_userNameLabel whc_LeftSpace:10 toView:_userImageView];
    [_userNameLabel whc_WidthAuto];
    [_userNameLabel whc_Height:18];
    
    [_comentimeLabel whc_TopSpace:0 toView:_userNameLabel];
    [_comentimeLabel whc_LeftSpaceEqualView:_userNameLabel];
    [_comentimeLabel whc_Size:CGSizeMake(130, 18)];
    _comentimeLabel.text = @"sdfasf";
    
    [_contentLabel whc_LeftSpaceEqualView:_comentimeLabel];
    [_contentLabel whc_TopSpace:5 toView:_comentimeLabel];
    [_contentLabel whc_RightSpace:18];
    [_contentLabel whc_HeightAuto];
        
    [_commentStackView whc_TopSpace:5 toView:_contentLabel];
    [_commentStackView whc_LeftSpaceEqualView:_contentLabel];
    [_commentStackView whc_RightSpace:18];
    [_commentStackView whc_AutoHeight];
    
    [_moreButton whc_TopSpace:0 toView:_commentStackView];
    [_moreButton whc_LeftSpaceEqualView:_commentStackView];
    [_moreButton whc_RightSpace:18];
    [_moreButton whc_Height:25];
    
    
    /// 配置评论展示容器
    _commentStackView.whc_Edge = UIEdgeInsetsMake(5, 5, 5, 5);  // 内边距为5
    _commentStackView.whc_Space = 5;                           // 评论之间的空隙为10
    _commentStackView.whc_Orientation = Vertical;               // 垂直布局
    
    _moreButton.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    
    /// 设置cell底部间隙
    self.whc_CellBottomView = _moreButton;
    self.whc_CellBottomOffset = 10;
    [self whc_BottomLine:0.3 lineColor:RGB(236, 237, 238)];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (void)setComment:(UILabel *)label answer:(ZCInvolvedDetailsModelChildComments *)answer userName:(NSString *)userName {
    NSString * text = nil;
    NSString *usernameStr = @"火星人";
    if (answer.username !=nil) {
        usernameStr = answer.username;
    }

    text = [NSString stringWithFormat:@"%@: %@",usernameStr,answer.content];
    label.text = text;
    NSMutableAttributedString * textAttr = [[NSMutableAttributedString alloc] initWithString:text];
    [textAttr addAttribute:NSForegroundColorAttributeName value:MainColor range:[text rangeOfString:usernameStr]];
    label.attributedText = textAttr;
}

/// 评论StackView和图片StackView视图复用处理
- (void)resetstackViewWithIsComment:(BOOL)isComment model:(ZCInvolvedDetailsModelHotComments *)friendModel {
    
    
    NSInteger oldCount = isComment ? _commentStackView.whc_SubViewCount : _imageStackView.whc_SubViewCount;
    NSInteger countDiff = isComment ? friendModel.childComments.count - oldCount : friendModel.imageArray.count - oldCount;
    if (countDiff < 0) {
        NSArray * subViewArray = isComment ? _commentStackView.subviews : _imageStackView.subviews;
        NSArray * removeCommentViewArray = [subViewArray subarrayWithRange:NSMakeRange(oldCount + countDiff, -countDiff)];
        for (UIView * view in removeCommentViewArray) {
            [view removeFromSuperview];
        }
        oldCount = isComment ? _commentStackView.whc_SubViewCount : _imageStackView.whc_SubViewCount;
    }
    countDiff = MAX(0, countDiff);
    for (int i = 0; i < oldCount; i++) {
        if (isComment) {
            ZCInvolvedDetailsModelChildComments * answer = friendModel.childComments[i];
            UILabel * commentLabel = _commentStackView.subviews[i];
            [self setComment:commentLabel answer:answer userName:friendModel.username];
        }else {
            UIImageView * imageView = _imageStackView.subviews[i];
            if (_model.imageArray.count > i &&
                [_model.imageArray[i] isEqualToString:friendModel.imageArray[i]]){}else {
                imageView.image = [UIImage imageNamed:friendModel.imageArray[i]];
            }
        }
    }
    for(int i = 0; i < countDiff; i++) {
        BOOL isNew = NO;
        if (isComment) {
            UILabel * commentLabel = (_commentLabelArray.count > oldCount + i ? _commentLabelArray[oldCount + i] : nil);
            if (commentLabel == nil) {
                isNew = YES;
                commentLabel = [UILabel new];
                commentLabel.backgroundColor = _commentStackView.backgroundColor;
                commentLabel.numberOfLines = 0;
                commentLabel.font = [UIFont systemFontOfSize:14];
            }
            ZCInvolvedDetailsModelChildComments * answer = friendModel.childComments[oldCount + i];
            [self setComment:commentLabel answer:answer userName:friendModel.username];
            if (isNew) {
                [_commentLabelArray addObject:commentLabel];
            }
            [_commentStackView addSubview:commentLabel];
        }else {
            UIImageView * imageView = (_imageViewArray.count > oldCount + i ? _imageViewArray[oldCount + i] : nil);
            if (imageView == nil) {
                isNew = YES;
                imageView = [UIImageView new];
                imageView.userInteractionEnabled = YES;
            }
            imageView.tag = oldCount + i;
            UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageGesture:)];
            [imageView addGestureRecognizer:tapGesture];
            imageView.image = [UIImage imageNamed:friendModel.imageArray[i + oldCount]];
            if(isNew) {
                [_imageViewArray addObject:imageView];
            }
            [_imageStackView addSubview:imageView];
        }
    }
    isComment ? [_commentStackView whc_StartLayout] : [_imageStackView whc_StartLayout];

//        NSInteger oldCount = isComment ? _commentStackView.whc_SubViewCount : _imageStackView.whc_SubViewCount;
//    
//    if ([friendModel.childComments isKindOfClass:[NSArray class]]) {
//    
//        NSInteger countDiff = isComment ? friendModel.childComments.count - oldCount : friendModel.imageArray.count - oldCount;
//        if (countDiff < 0) {
//            NSArray * subViewArray = isComment ? _commentStackView.subviews : _imageStackView.subviews;
//            NSArray * removeCommentViewArray = [subViewArray subarrayWithRange:NSMakeRange(oldCount + countDiff, -countDiff)];
//            for (UIView * view in removeCommentViewArray) {
//                [view removeFromSuperview];
//            }
//            oldCount = isComment ? _commentStackView.whc_SubViewCount : _imageStackView.whc_SubViewCount;
//        }
//        countDiff = MAX(0, countDiff);
//        for (int i = 0; i < oldCount; i++) {
//            if (isComment) {
//                ZCInvolvedDetailsModelChildComments * answer = friendModel.childComments[i];
//                UILabel * commentLabel = _commentStackView.subviews[i];
//                [self setComment:commentLabel answer:answer userName:friendModel.username];
//            }else {
//                UIImageView * imageView = _imageStackView.subviews[i];
//                if (_model.imageArray.count > i &&
//                    [_model.imageArray[i] isEqualToString:friendModel.imageArray[i]]){}else {
//                    imageView.image = [UIImage imageNamed:friendModel.imageArray[i]];
//                }
//            }
//        }
//        for(int i = 0; i < countDiff; i++) {
//            BOOL isNew = NO;
//            if (isComment) {
//                UILabel * commentLabel = (_commentLabelArray.count > oldCount + i ? _commentLabelArray[oldCount + i] : nil);
//                if (commentLabel == nil) {
//                    isNew = YES;
//                    commentLabel = [UILabel new];
//                    commentLabel.backgroundColor = _commentStackView.backgroundColor;
//                    commentLabel.numberOfLines = 0;
//                    commentLabel.font = [UIFont systemFontOfSize:14];
//                }
//                ZCInvolvedDetailsModelChildComments * answer = friendModel.childComments[oldCount + i];
//                [self setComment:commentLabel answer:answer userName:friendModel.username];
//                if (isNew) {
//                    [_commentLabelArray addObject:commentLabel];
//                }
//                [_commentStackView addSubview:commentLabel];
//            }else {
//                UIImageView * imageView = (_imageViewArray.count > oldCount + i ? _imageViewArray[oldCount + i] : nil);
//                if (imageView == nil) {
//                    isNew = YES;
//                    imageView = [UIImageView new];
//                    imageView.userInteractionEnabled = YES;
//                }
//                imageView.tag = oldCount + i;
//                UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageGesture:)];
//                [imageView addGestureRecognizer:tapGesture];
//                imageView.image = [UIImage imageNamed:friendModel.imageArray[i + oldCount]];
//                if(isNew) {
//                    [_imageViewArray addObject:imageView];
//                }
//                [_imageStackView addSubview:imageView];
//            }
//        }
//        isComment ? [_commentStackView whc_StartLayout] : [_imageStackView whc_StartLayout];
//    }
    
}


-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
}

-(void)setFriendModel:(ZCInvolvedDetailsModelHotComments *)friendModel{
    

    
//    NSLog(@"sdfasdfa = %@ ",friendModel);
    [_userImageView sd_setImageWithURL:[NSURL URLWithString:friendModel.avatar] placeholderImage:[UIImage imageNamed:@"projectdetail_icon"]];
    _userNameLabel.text = friendModel.username;
    _contentLabel.text = friendModel.content;
    
    int timeInt =[friendModel.ctime intValue];
    _comentimeLabel.text = [NSString stringWithFormat:@"%@",stringDataWithTimeInterval(timeInt)];
    
    if ([friendModel.likecount integerValue]>99) {
        _menuView.supperLable.text = [NSString stringWithFormat:@"99+"];
    }else{
        _menuView.supperLable.text = friendModel.likecount;
    }
    
    if ([friendModel.commentCount integerValue]>99) {
        _menuView.answerLable.text = [NSString stringWithFormat:@"99+"];
    }else{
        _menuView.answerLable.text = friendModel.commentCount;
    }
    
    if (friendModel.childComments.count<3) {
        _moreButton.hidden = YES;
        self.whc_CellBottomView = _commentStackView;
    }else{
        _moreButton.hidden = NO;
        self.whc_CellBottomView = _moreButton;
    }
    [self resetstackViewWithIsComment:YES model:friendModel];
    _model = nil;
    _model = friendModel;
}

#pragma mark - buttonAction -

- (void)tapImageGesture:(UITapGestureRecognizer *)tapGesture {
    NSInteger column = 3;
    if (_model.imageArray.count == 1) {
        column = 2;
    }
//    [WHC_ImageDisplayView show:_model.imageArray index:tapGesture.view.tag item:tapGesture.view column:column];
}

#pragma mark - AnswerMenuViewDelegate -

-(void)superMenuView:(AnswerMenuView *)menuView isClickSupper:(BOOL)isSupper{
    if (UserModel.password.length>1) {
        NSInteger num = [_model.likecount integerValue];
        _model.islick = YES;
        num++;
        if (num>99) {
            _menuView.supperLable.text = [NSString stringWithFormat:@"99+"];
        }else{
            _menuView.supperLable.text = [NSString stringWithFormat:@"%ld",(long)num];
        }
        _model.likecount = [NSString stringWithFormat:@"%ld",(long)num];
        _menuView.supperView.userInteractionEnabled = NO;
        UIImage *image = _model.islick ? [UIImage imageNamed:@"projectdetail_zan_yes"] : [UIImage imageNamed:@"projectdetail_zan"];
        
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
            _menuView.supperImg.layer.transformScale = 1.7;
            _menuView.supperNum.alpha = 1;
            _menuView.supperNum.textColor = [UIColor colorWithHexString:@"#ffdd00"];
        } completion:^(BOOL finished) {
            [_menuView.supperImg setImage:image];
            [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
                _menuView.supperImg.layer.transformScale = 0.9;
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
                    _menuView.supperNum.alpha = 0;
                    _menuView.supperImg.layer.transformScale = 1;
                    
                } completion:^(BOOL finished) {
                }];
            }];
        }];
        if (_delegate && [ _delegate respondsToSelector:@selector(likeButtonClickindex:islogin:)]) {
            [_delegate likeButtonClickindex:self.indexPath islogin:YES];
        }
    }else{
        if (_delegate && [ _delegate respondsToSelector:@selector(likeButtonClickindex:islogin:)]) {
            [_delegate likeButtonClickindex:self.indexPath islogin:NO];
        }
    }
}

- (void)answerMenuView:(AnswerMenuView *)menuView isClickSupper:(BOOL)isSupper {
    if (_delegate && [ _delegate respondsToSelector:@selector(commentButtonClickindex:)]) {
        [_delegate commentButtonClickindex:self.indexPath];
    }
}

@end
